import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:note/common/common.dart';
import 'package:note/pages/note_favorite/view.dart';
import 'package:note/pages/note_list_page/view.dart';
import 'package:note/widgets/BasePage.dart';
import 'package:note/widgets/rootDrawer.dart';
import 'state.dart';
import 'logic.dart';

class HomePage extends StatelessWidget {
  HomePage({Key? key}) : super(key: key);

  final logic = Get.put(HomePageLogic());
  final state = Get.find<HomePageLogic>().state;

  @override
  Widget build(BuildContext context) {
    return BasePage(
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Styles.backgroundColor,
          foregroundColor: Styles.mainThemeColor,
          shadowColor: Styles.shadowColor,
          leading: logic.openDraw(),
          actions: [
            IconButton(
                onPressed: logic.onSearch, icon: const Icon(Icons.search)),
            IconButton(
                onPressed: logic.onUploadMd,
                icon: const Icon(Icons.upload_rounded)),
            IconButton(onPressed: logic.onAddNote, icon: const Icon(Icons.add)),
          ],
          title: DropdownButtonHideUnderline(
            child: Obx(() {
              return DropdownButton(
                autofocus: false,
                focusColor: Styles.transparent,
                value: state.pageName.value,
                onChanged: (value) {
                  state.pageName.value = value ?? PageName.allNoteList;
                },
                iconSize: 30,
                iconEnabledColor: Styles.mainThemeColor,
                items: [
                  defaultDropdownMenuItem(
                      value: PageName.allNoteList, text: Strings.noteListTitle),
                  defaultDropdownMenuItem(
                      value: PageName.favoriteNoteList,
                      text: Strings.favoriteNoteList),
                ],
                elevation: 24,
                //设置阴影的高度
                style: Styles.textThemeMedium(20),
                //isDense: false,//减少按钮的高度。默认情况下，此按钮的高度与其菜单项的高度相同。如果isDense为true，则按钮的高度减少约一半。 这个当按钮嵌入添加的容器中时，非常有用
                dropdownColor: Styles.backgroundColor,
              );
            }),
          ),
        ),
        drawer: const RootDrawer(),
        body: Obx(() {
          if (state.pageName.value == PageName.allNoteList) {
            return NoteListPage();
          } else if (state.pageName.value == PageName.favoriteNoteList) {
            return NoteFavoritePage();
          }
          return Container();
        }),
        floatingActionButton: GestureDetector(
          onTap: logic.onAddNote,
          child: Container(
            width: 50,
            height: 50,
            margin: const EdgeInsets.only(right: 10, bottom: 10),
            decoration: const BoxDecoration(
                color: Styles.mainThemeColor,
                boxShadow: [
                  BoxShadow(color: Styles.shadowColor, blurRadius: 5)
                ],
                borderRadius: BorderRadius.all(Radius.circular(25))),
            child: const Icon(
              Icons.add,
              color: Styles.lightColor,
              size: 30,
            ),
          ),
        ),
      ),
    );
  }

  DropdownMenuItem defaultDropdownMenuItem(
      {required PageName value, required String text}) {
    return DropdownMenuItem(
        value: value, alignment: Alignment.center, child: Text(text));
  }
}
